package org.qpcr.community.project.web.excel;

import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;
import org.hswebframework.reactor.excel.CellDataType;
import org.hswebframework.reactor.excel.ExcelHeader;
import org.hswebframework.web.authorization.Authentication;
import org.hswebframework.web.bean.FastBeanCopier;
import org.hswebframework.web.validator.ValidatorUtils;
import org.qpcr.community.project.model.entity.TestSequenceInstanceEntity;
import org.qpcr.core.metadata.Jsonable;

import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Getter
@Setter
public class TestSequenceExcelInfo implements Jsonable {

    @org.qpcr.community.io.excel.annotation.ExcelHeader(value = "序列名称")
    @NotBlank(message = "序列名称不能为空")
    private String name;

    @org.qpcr.community.io.excel.annotation.ExcelHeader(value = "序列编码")
    @NotBlank(message = "序列编码不能为空")
    private String code;

    @org.qpcr.community.io.excel.annotation.ExcelHeader(value = "序列步骤")
    @NotBlank(message = "序列步骤不能为空")
    private String step;

    private TestSequenceInstanceEntity testSequenceInstance;

    private Map<String, Object> configuration = new HashMap<>();

    private long rowNumber;

    private String state;

    public Map<String,Object> toMap(){
        return FastBeanCopier.copy(this,new HashMap<>());
    }

    public static List<ExcelHeader> getTemplateHeaderMapping(TestSequenceExcelFilterColumns filterColumns) {
        return Arrays.stream(new ExcelHeader[]{
                new ExcelHeader("name", "序列名称", CellDataType.STRING),
                new ExcelHeader("code", "序列编码", CellDataType.STRING),
                new ExcelHeader("step", "序列步骤", CellDataType.STRING)
            })
            .filter(a-> !filterColumns.getColumns().contains(a.getKey()))
            .collect(Collectors.toList());
    }

    public static List<ExcelHeader> getExportHeaderMapping(TestSequenceExcelFilterColumns filterColumns) {
        return Arrays.stream(new ExcelHeader[]{
                new ExcelHeader("name", "序列名称", CellDataType.STRING),
                new ExcelHeader("code", "序列编码", CellDataType.STRING),
                new ExcelHeader("step", "序列步骤", CellDataType.STRING)
            })
            .filter(a-> !filterColumns.getColumns().contains(a.getKey()))
            .collect(Collectors.toList());
    }

    /**
     * 初始化保存实体
     *
     * @param testSequenceInstanceListDb 数据库已存在的数据
     * @param auth 认证数据
     * @return TestSequenceExcelInfo
     */
    public TestSequenceExcelInfo initTestSequenceInstance(List<TestSequenceInstanceEntity> testSequenceInstanceListDb, Authentication auth) {
        List<TestSequenceInstanceEntity> list = testSequenceInstanceListDb.stream()
                .filter(db -> db.getName() != null && db.getName().equals(this.name)
                        || db.getCode() != null && db.getCode().equals(this.getCode()))
                .collect(Collectors.toList());
        if (list.size() > 0) {
            throw new RuntimeException("序列名称或序列编号已存在");
        }
        TestSequenceInstanceEntity entity = FastBeanCopier.copy(this, new TestSequenceInstanceEntity());

        entity.setCreateTimeNow();
        entity.setCreatorId(auth.getUser().getId());
        entity.setCreatorName(auth.getUser().getName());

        entity.setModifyTimeNow();
        entity.setModifierId(auth.getUser().getId());
        entity.setModifierName(auth.getUser().getName());
        entity.setType(0);

        ValidatorUtils.tryValidate(entity);

        this.testSequenceInstance = entity;
        return this;
    }

    @Override
    public void fromJson(JSONObject json) {
        Jsonable.super.fromJson(json);
    }
}
